package exception;

import java.nio.file.FileAlreadyExistsException;

public class TestProgram {

	public static void main(String[] args) {
		int step = 7;
		switch (step) {
		case 1:
			System.out.println(returnInTryBlock());
			break;
		case 2:
			System.out.println(returnInCatchBlock());
			break;
		case 3:
			System.out.println(returnInCatchAndFinally());
			break;
		case 4:
			System.out.println(returnInTryAndFinally());
			break;
		case 5:
			System.out.println(modifyReturnValue1());
			break;
		case 6:
			System.out.println(modifyReturnValue2());
			break;
		case 7:
			handleError();
			break;
		}

	}

	public static int returnInTryBlock() {
		try {
			System.out.println("Try block");
			int a = 5;
			return a;
		} finally {
			System.out.println("Finally");
		}
	}

	public static double returnInCatchBlock() {
		try {
			System.out.println("Try block");
			int a = 5;
			return a / 0;
		} catch (Exception e) {
			System.out.println("Catch block");
			return 100;
		} finally {
			System.out.println("Finally block");
		}
	}

	public static double returnInCatchAndFinally() {
		try {
			String emp[] = { "Tom", "Jerry" };
			System.out.println(emp[5]);
		} catch (ArrayIndexOutOfBoundsException ex) {
			return 10;
		} finally {
			return 20;
		}
	}

	public static double returnInTryAndFinally() {
		try {
			return 10;
		} finally {
			return 20;
		}
	}

	public static int modifyReturnValue1() {
		int returnVal = 10;
		try {
			String[] students = { "Harry", "Paul" };
			System.out.println(students[5]);
		} catch (Exception e) {
			System.out.println("About to return :" + returnVal);
			return returnVal;
		} finally {
			returnVal += 10;
			System.out.println("Return value is now :" + returnVal);
		}
		return returnVal;
	}

	public static StringBuilder modifyReturnValue2() {
		StringBuilder returnVal = new StringBuilder("10");
		try {
			String[] students = { "Harry", "Paul" };
			System.out.println(students[5]);
		} catch (Exception e) {
			System.out.println("About to return :" + returnVal);
			return returnVal;
		} finally {
			returnVal.append("10");
			System.out.println("Return value is now :" + returnVal);
		}
		return returnVal;
	}

	public static void handleError() {
		try {
			myMethod();
		} catch (StackOverflowError s) {
			for (int i = 0; i < 2; ++i)
				System.out.println(i);
		}
	}

	public static void myMethod() {
		myMethod();
	}
}
